package com.cloud.controller.menu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.bean.good.Good;
import com.cloud.bean.menu.Menu;
import com.cloud.bean.user.User;
import com.cloud.common.BaseReturn;
import com.cloud.service.good.GoodService;
import com.cloud.service.menu.MenuService;
import com.cloud.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import static com.cloud.common.JwtUtils.getByToken;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyuan
 * @since 2022-07-26
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    GoodService goodService;

    @RequestMapping("/list")
    public BaseReturn list(@RequestBody Menu menu){
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","用户异常请你联系管理员");
        }
        String customerNo = user.getCustomerNo();
        if(customerNo == null){
            return BaseReturn.build("5000","请你联系管理员,绑定商户号");
        }
        menu.setCustomerNo(customerNo);
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>(menu);
        List<Menu> list = menuService.list(queryWrapper);
        return BaseReturn.build("2000","查询成功",list);
    }
    @RequestMapping("/goodsListByMenu")
    public BaseReturn goodsListByMenu(){
        Menu menu = new Menu();
        User user = getByToken();
        if(user == null){
            return BaseReturn.build("4003","用户异常请你联系管理员");
        }
        String customerNo = user.getCustomerNo();
        if(customerNo == null){
            return BaseReturn.build("5000","请你联系管理员,绑定商户号");
        }
        menu.setCustomerNo(customerNo);
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>(menu);
        List<Menu> lists = menuService.list(queryWrapper);
        List<MenuData> menuDataList = new ArrayList<>();
        for (Menu item: lists) {
            MenuData menuData = new MenuData();
            List<GoodsData> goodsDataList = new ArrayList<>();
            menuData.setValue(item.getId());
            menuData.setLabel(item.getName());
            HashMap<String, Object> queryMap = new HashMap<>();
            queryMap.put("customer_no",customerNo);
            queryMap.put("menu_id",item.getId());
            Collection<Good> goods = goodService.listByMap(queryMap);
            for (Good good:goods) {
                GoodsData goodsData = new GoodsData();
                goodsData.setValue(good.getId());
                goodsData.setLabel(good.getName());
                goodsDataList.add(goodsData);
            }
            menuData.setChildren(goodsDataList);
            menuDataList.add(menuData);
        }
        return BaseReturn.build("2000","",menuDataList);


    }


}

